﻿using AccompanyCabinet.Helper;
using AccompanyCabinet.Model;
using Google.Protobuf;
using Newtonsoft.Json;
using SocketCmd;
using SocketCS;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace AccompanyCabinet
{
    public partial class StartUp : Form
    {
        public StartUp()
        {
            InitializeComponent();
        }

        private void StartUp_Load(object sender, EventArgs e)
        {
            #region 测试代码
            //Socket测试
            SocketHelper.SocketConnect();
            SocketHelper.client.OnReceive += new TcpClientEvent.OnReceiveEventHandler(Front.index.SocketReceive);
            var register = new Register
            {
                CmdCode = 0x0101,
                Identity = ConfigurationManager.AppSettings["Identity"].ToString(),
                TimeToken = (ulong)(DateTime.Now - new DateTime(1970, 1, 1)).TotalMilliseconds,
                SoftVersion = "1.0",
                HardVersion = "1.0"
            };

            SocketHelper.SocketSend(register.ToByteArray());
            //SocketHelper.SocketSend("2221111");
            //HttpPost测试
            //Dictionary<string, string> parameters = new Dictionary<string, string>();
            //parameters.Add("aa","11");
            //parameters.Add("bb", "22");
            //string url = "http://localhost:51406/User/ApiTest";
            //string response = HttpHelper.Post(parameters, url, 10);
            ////HttpGet测试
            //string urlGet = "http://localhost:51406/User/ApiTestGet";
            //string response1 = HttpHelper.Get(urlGet+"?aa=x&bb=测试的中文");
            #endregion
            int a = 0, b = 0;
            string identity = ConfigurationManager.AppSettings["Identity"].ToString();
            do
            {
                string url = "TerminalInfo/DevBoxAllDetail";
                var res = HttpHelper.Get(url + "?identity=" + identity + "");
                if (res.status == "OK")
                {
                    HttpHelper.allDeviceBoxDetail = JsonConvert.DeserializeObject<AllDeviceBoxDetail>(res.Message);
                    HttpHelper.costDetailList = HttpHelper.allDeviceBoxDetail.priceDetails;
                    HttpHelper.deviceInfoModel = HttpHelper.allDeviceBoxDetail.deviceBoxDetailto;
                    HttpHelper.dBoxLists = HttpHelper.allDeviceBoxDetail.dBoxLists;
                    if (ConfigurationManager.AppSettings["dataStatus"] == "GET")
                    {
                        CreateXml();
                    }
                    a = 1;
                }
                b++;
                if (b == 3) return;
            } while (a == 0);

            ///测试时间加减
            //TimeSpan timeSpan = DateTime.Now - Convert.ToDateTime("2018-11-26 12:12")/*.AddHours(Convert.ToInt32(Duration))*/;
            //if (timeSpan.TotalMinutes > 0)
            //{
            //    int overtimecount = Convert.ToInt32(timeSpan.TotalMinutes) / 60 > 0 ? Convert.ToInt32(Convert.ToInt32(timeSpan.TotalMinutes) / 60) : 1;
            //}
            
        }

        void CreateXml()
        {
            XmlDocument xmlDoc = new XmlDocument();//新建XML文件
            XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
            xmlDoc.AppendChild(node);
            //创建根节点    
            XmlNode root = xmlDoc.CreateElement("DevieceMessages");
            xmlDoc.AppendChild(root);

            #region deviceModel
            //创建子节点  柜子节点
            XmlElement xmle = xmlDoc.CreateElement("Device");
            root.AppendChild(xmle);
            //子节点添加内容
            XmlElement DId = xmlDoc.CreateElement("DId");
            DId.InnerText = HttpHelper.deviceInfoModel.DId.ToString();
            xmle.AppendChild(DId);

            XmlElement Name = xmlDoc.CreateElement("Name");
            Name.InnerText = HttpHelper.deviceInfoModel.Name;
            xmle.AppendChild(Name);

            XmlElement LineState = xmlDoc.CreateElement("LineState");
            LineState.InnerText = HttpHelper.deviceInfoModel.LineState;
            xmle.AppendChild(LineState);

            XmlElement UseTypeNo = xmlDoc.CreateElement("UseTypeNo");
            UseTypeNo.InnerText = HttpHelper.deviceInfoModel.UseTypeNo;
            xmle.AppendChild(UseTypeNo);

            XmlElement Type = xmlDoc.CreateElement("Type");
            Type.InnerText = HttpHelper.deviceInfoModel.Type;
            xmle.AppendChild(Type);

            XmlElement ExpireTime = xmlDoc.CreateElement("ExpireTime");
            ExpireTime.InnerText = HttpHelper.deviceInfoModel.ExpireTime.ToString("yyyy-MM-dd");
            xmle.AppendChild(ExpireTime);

            XmlElement Address = xmlDoc.CreateElement("Address");
            Address.InnerText = HttpHelper.deviceInfoModel.Address;
            xmle.AppendChild(Address);
            #endregion

            #region deviceboxlist
            ///箱子节点
            XmlElement xmbox = xmlDoc.CreateElement("Boxs");
            root.AppendChild(xmbox);

            for (int i = 0; i < HttpHelper.dBoxLists.Count; i++)
            {
                XmlElement xmlbox = xmlDoc.CreateElement("box");
                xmbox.AppendChild(xmlbox);

                XmlElement id = xmlDoc.CreateElement("id");
                id.InnerText = HttpHelper.dBoxLists[i].id.ToString();
                xmlbox.AppendChild(id);

                XmlElement Num = xmlDoc.CreateElement("Num");
                Num.InnerText = HttpHelper.dBoxLists[i].Num.ToString();
                xmlbox.AppendChild(Num);

                XmlElement LockId = xmlDoc.CreateElement("LockId");
                LockId.InnerText = HttpHelper.dBoxLists[i].LockId.ToString();
                xmlbox.AppendChild(LockId);

                XmlElement LockNum = xmlDoc.CreateElement("LockNum");
                LockNum.InnerText = HttpHelper.dBoxLists[i].LockNum.ToString();
                xmlbox.AppendChild(LockNum);

                XmlElement TypeNo = xmlDoc.CreateElement("TypeNo");
                TypeNo.InnerText = HttpHelper.dBoxLists[i].TypeNo.ToString();
                xmlbox.AppendChild(TypeNo);

                XmlElement UpdateTime = xmlDoc.CreateElement("UpdateTime");
                UpdateTime.InnerText = HttpHelper.dBoxLists[i].UpdateTime.ToString();
                xmlbox.AppendChild(UpdateTime);

                XmlElement StatusNo = xmlDoc.CreateElement("StatusNo");
                StatusNo.InnerText = HttpHelper.dBoxLists[i].StatusNo.ToString();
                xmlbox.AppendChild(StatusNo);

                XmlElement FingerPrint = xmlDoc.CreateElement("FingerPrint");
                FingerPrint.InnerText = "";
                xmlbox.AppendChild(FingerPrint);

                XmlElement OrderNum = xmlDoc.CreateElement("OrderNum");
                OrderNum.InnerText = "";
                xmlbox.AppendChild(OrderNum);

                XmlElement StartTime = xmlDoc.CreateElement("StartTime");
                StartTime.InnerText = "";
                xmlbox.AppendChild(StartTime);

                XmlElement Duration = xmlDoc.CreateElement("Duration");
                Duration.InnerText = "";
                xmlbox.AppendChild(Duration);

                XmlElement OverTimePrice = xmlDoc.CreateElement("OverTimePrice");
                OverTimePrice.InnerText = "";
                xmlbox.AppendChild(OverTimePrice);

                XmlElement OrderId = xmlDoc.CreateElement("OrderId");
                OrderId.InnerText = "";
                xmlbox.AppendChild(OrderId);
            }
            #endregion

            #region costdetaillist
            ///箱子费用节点
            XmlElement xmcost = xmlDoc.CreateElement("BoxCosts");
            root.AppendChild(xmcost);

            for (int i = 0; i < HttpHelper.costDetailList.Count; i++)
            {
                XmlElement cost = xmlDoc.CreateElement("cost");
                xmcost.AppendChild(cost);

                XmlElement DpId = xmlDoc.CreateElement("DpId");
                DpId.InnerText = HttpHelper.costDetailList[i].DpId.ToString();
                cost.AppendChild(DpId);

                XmlElement Price = xmlDoc.CreateElement("Price");
                Price.InnerText = HttpHelper.costDetailList[i].Price.ToString();
                cost.AppendChild(Price);

                XmlElement Duration = xmlDoc.CreateElement("Duration");
                Duration.InnerText = HttpHelper.costDetailList[i].Duration.ToString();
                cost.AppendChild(Duration);

                XmlElement TypeNo = xmlDoc.CreateElement("TypeNo");
                TypeNo.InnerText = HttpHelper.costDetailList[i].TypeNo.ToString();
                cost.AppendChild(TypeNo);

                XmlElement PriceType = xmlDoc.CreateElement("PriceType");
                PriceType.InnerText = HttpHelper.costDetailList[i].PriceType.ToString();
                cost.AppendChild(PriceType);
            }
            #endregion
            xmlDoc.Save("../DeviceData.xml");
        }



        #region 心跳包

        private void HeartBeat_Tick(object sender, EventArgs e)
        {

        }
        #endregion



        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            new Front.index().Show();
        }
    }
}
